package org.hackystat.devcathlon.ui.page.scoreboard.halloffame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;

/**
 * Display the Most Popular events to the Hall of Fame.
 * 
 * @author John Ancheta, Scheller Sanchez
 */
public class MostPopularEventsPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * This Panel displays the Most Popular Events for Hall of Fame.
   * 
   * @param id The Wicket id from the html markup.
   */
  public MostPopularEventsPanel(String id) {
    super(id);
    
    final HashMap<String, Integer> mapMostPopularEvents = new HashMap<String, Integer>();
    Collection<Match> matches = MatchManager.getInstance().getAllMatches();
    
    for (Match match : matches) {
      
      List<Event> events = match.getEvents();
      for (Event event : events) {
        String eventName = event.getName();
        int eventCount = 1;
        if (mapMostPopularEvents.containsKey(eventName)) {
          eventCount += mapMostPopularEvents.get(eventName).intValue();
        }
        mapMostPopularEvents.put(eventName, Integer.valueOf(eventCount));
      } 
    }
    
    final List<String> listMostPopularEvents = new ArrayList<String>();
    Set<Map.Entry<String, Integer>> mapentryMapMostPopularEvents = mapMostPopularEvents.entrySet();
    
    int rank = 0; // The rank of the event.
    
    while (!mapentryMapMostPopularEvents.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostPopularEvents) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      rank++;
      listMostPopularEvents.add(rank + ".  " + most.getKey() + " (" + 
          most.getValue().toString() + " count)");
      if (DevcathlonSession.get().getEmail() == null) {
        mapentryMapMostPopularEvents.clear();
      }
      else {
        mapentryMapMostPopularEvents.remove(most);
      }
    }
    
    DataView dataview = new DataView("ItemList", new ListDataProvider(listMostPopularEvents)) {
      private static final long serialVersionUID = 1L;
      
      @Override
      protected void populateItem(Item item) {
        final String popularEvent = (String) item.getModelObject();
        item.add(new Label("labelMostPopularEvents", popularEvent));
      }
      
      
    };
    
    add(dataview);
    
    
    
  }
}
